package com.concerneyes.mine.TabActivity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.concerneyes.R;
import com.concerneyes.mine.TabActivity.serviceCentre.CheckLog;
import com.concerneyes.mine.TabActivity.serviceCentre.CommonProblem;
import com.concerneyes.mine.TabActivity.serviceCentre.KnowledgeBase;
import com.concerneyes.mine.TabActivity.serviceCentre.OnLineService;
import com.concerneyes.mine.TabActivity.serviceCentre.Shop;
import com.concerneyes.mine.adapter.MySeverCentreAdapter;

import common.AbsActivity;
import util.ActivityUtil;

import static com.concerneyes.R.id.lv_server_center;

/**
 * 服务中心
 */

public class MyServiceCentre extends AbsActivity implements AdapterView.OnItemClickListener{
    private ListView lvServerCenter;
    private MySeverCentreAdapter adapter;
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_serivce_centre);
        String title = getIntent().getExtras().getString("title");
        setTitle(title);
        initView();
    }

    private void initView() {
        lvServerCenter = (ListView) findViewById(lv_server_center);
        adapter = new MySeverCentreAdapter(this);
        lvServerCenter.setAdapter(adapter);
        lvServerCenter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String[] title = getResources().getStringArray(R.array.mine_sever_center_item_title);

        Bundle bundle = new Bundle();
        bundle.putString("title",title[position]);
        switch (position){
            case 0:
                ActivityUtil.setIntent(this, CommonProblem.class,bundle);
                break;
            case 1:
                ActivityUtil.setIntent(this, KnowledgeBase.class,bundle);
                break;
            case 2:
                ActivityUtil.setIntent(this, CheckLog.class,bundle);
                break;
            case 4:
                ActivityUtil.setIntent(this, Shop.class,bundle);
                break;
            case 5:
                ActivityUtil.setIntent(this, OnLineService.class,bundle);
                break;
            case 6:
                showPhoneDialog();
                break;
        }
    }


    private void showPhoneDialog() {
        final Dialog dialog = new Dialog(this, R.style.Theme_Dialog_Translucent);
        dialog.setCanceledOnTouchOutside(true);
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.dialog_phone, null);
        dialog.setContentView(view);
        dialog.show();

        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        int width = getResources().getDisplayMetrics().widthPixels;
        lp.width = width;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);

        Button phoneBtn = (Button) view.findViewById(R.id.phoneBtn);
        phoneBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + "10086");
                intent.setData(data);
                startActivity(intent);
            }
        });

        Button smsBtn = (Button) view.findViewById(R.id.smsBtn);
        smsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + "10086"));
                intent.putExtra("sms_body", "");
                startActivity(intent);
            }
        });

        Button cancelBtn = (Button) view.findViewById(R.id.cancelBtn);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

}
